# coding:utf-8
from odoo import models, fields, api, _
from odoo.osv import expression


class OLResPartner(models.Model):
    _name = 'res.partner'
    _inherit = 'res.partner'
    # need update name in res.partner make correct show in partner list.

    @api.multi
    def name_get(self):
        res = []
        for partner in self:
            name = partner.ref and '[%s] %s' % (partner.ref, partner.name) or partner.name or ''

            if partner.company_name or partner.parent_id:
                if not name and partner.type in ['invoice', 'delivery', 'other']:
                    name = dict(self.fields_get(['type'])['type']['selection'])[partner.type]
                if not partner.is_company:
                    name = partner.parent_id.ref \
                           and "[%s] %s, %s" % (
                    partner.parent_id.ref, partner.commercial_company_name or partner.parent_id.name, name) \
                           or "%s, %s" % (partner.commercial_company_name or partner.parent_id.name, name)
            if self._context.get('show_address_only'):
                name = partner._display_address(without_company=True)
            if self._context.get('show_address'):
                name = name + "\n" + partner._display_address(without_company=True)
            name = name.replace('\n\n', '\n')
            name = name.replace('\n\n', '\n')
            if self._context.get('show_email') and partner.email:
                name = "%s <%s>" % (name, partner.email)
            if self._context.get('html_format'):
                name = name.replace('\n', '<br/>')
            res.append((partner.id, name))
        return res
